package com.rao.gmall.cart.api;

import com.rao.gmall.cart.service.CartService;
import com.rao.gmall.common.result.Result;
import com.rao.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author nzmxfan
 * @create 2021-08-17-18:35
 */
@RestController
@RequestMapping("/api/cart")
public class CartInfoController {

    @Autowired
    CartService cartService;

    /**
     * 向购物车中添加商品
     *
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/addToCart")
    public CartInfo addToCart(@RequestParam("skuId") Long skuId,
                              @RequestParam("skuNum") Integer skuNum,
                              HttpServletRequest request) {

        /**
         * 告诉我们操作哪个用户的购物车
         * 用户登录了；有Cookie
         *      token：
         * 用户没登录：有Cookie
         *      userTempId：
         *
         */
        // key 为 cart:info:userId   或者  cart:temp:userTempId
        String key = cartService.determinCartKey(request);
        // 返回参数：skuId、skuName、price、skuNum、skuDefaultImageUrl
        CartInfo cartInfo = cartService.addToCart(key, skuId, skuNum);
        return cartInfo;
    }

    /**
     * 通过 ajax 请求向购物车中添加商品
     *
     * //ajax请求过了网关。网关会自动提取并透传用户id以及临时id
     * @param skuId
     * @param skuNum
     * @return
     */
    @PostMapping("/addToCart/{skuId}/{skuNum}")
    public Result ajaxAddToCart(@PathVariable("skuId") Long skuId,
                                  @PathVariable("skuNum") Integer skuNum,
                                  HttpServletRequest request) {

        // key 为 cart:info:userId   或者  cart:temp:userTempId
        String key = cartService.determinCartKey(request);
        // 返回参数：skuId、skuName、price、skuNum、skuDefaultImageUrl
        cartService.addToCart(key, skuId, skuNum);

        return Result.ok();
    }

    /**
     * 查看是否登录
     * 如果已登录：将临时购物车写入用户购物车，并清空临时购物车，最后返回所有结果集；
     * 如果未登录：直接返回所有结果集；
     *
     * @param request
     * @return
     */
    @GetMapping("/cartList")
    public Result getCartInfoList(HttpServletRequest request) {

        // 获取临时购物车
        String tempCartKey = cartService.tempCartKey(request);
        // 获取用户购物车
        String userCartKey = cartService.userCartKey(request);
        // 查询所有购物车的数据
        List<CartInfo> cartInfos = cartService.listCart(userCartKey, tempCartKey);

        return Result.ok(cartInfos);
    }

    // 设置勾选状态
    @GetMapping("/checkCart/{skuId}/{checked}")
    public Result ajaxCheckCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("checked") Integer checked,
                                HttpServletRequest request) {
        String key = cartService.determinCartKey(request);

        cartService.ajaxCheckCart(key, skuId, checked);

        return Result.ok();
    }

    /**
     * 根据 skuId 删除购物项
     * @param skuId
     * @return
     */
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCartInfo(@PathVariable("skuId") Long skuId,
                                 HttpServletRequest request){
        // 获取购物车的 key
        String key = cartService.determinCartKey(request);
        // 根据 skuId 删除对应的购物项
        cartService.deleteCartInfo(key, skuId);
        return Result.ok();
    }

    @GetMapping("/isCheckList")
    public List<CartInfo> getCartIsCheckList(HttpServletRequest request){
        String key = cartService.determinCartKey(request);
        return cartService.listCheckCartInfo(key);
    }

    @DeleteMapping("/deleteBySkuIds")
    public Result deleteSkuFromCart(@RequestParam List<String> skuIds,
                             HttpServletRequest request){

        String key = cartService.determinCartKey(request);

        cartService.deleteBatch(key, skuIds);
        return Result.ok();
    }
}